home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / applic / ncsa / Mac / Telnet2.6 / prerelease / d3 / Telnet2.6.1d3.src.sit.hqx / Telnet 2.6.1d3 source / source / init / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-13  |  12.6 KB  |  463 lines

  1. /*
  2. *    init.c
  3. *    Top level of init code & general initialization routines
  4. *    Called only by main()
  5. *
  6. *****************************************************************
  7. *    NCSA Telnet for the Macintosh                                *
  8. *                                                                *
  9. *    National Center for Supercomputing Applications                *
  10. *    Software Development Group                                    *
  11. *    152 Computing Applications Building                            *
  12. *    605 E. Springfield Ave.                                        *
  13. *    Champaign, IL  61820                                        *
  14. *                                                                *
  15. *    Copyright (c) 1986-1992,                                    *
  16. *    Board of Trustees of the University of Illinois                *
  17. *****************************************************************
  18. *  Revisions:
  19. *  7/92        Telnet 2.6:    Initial version.  Jim Browne
  20. */
  21.  
  22. #ifdef MPW
  23. #pragma segment INIT
  24. #endif
  25.  
  26. #include "TelnetHeader.h"
  27.  
  28. #include <string.h>
  29. #include <Folders.h>
  30. #include <Script.h>
  31. #include <stdio.h>
  32. #include <GestaltEqu.h>
  33. #include <AppleEvents.h>
  34.  
  35. #include "init.proto.h"
  36. #include "debug.h"
  37. #include "mainseg.proto.h"        /* For forcequit() proto */
  38. #include "iNetwork.proto.h"    /* For initnet proto */
  39. #include "macros.proto.h"        /* For setmacro proto */
  40. #include "wind.h"        /* For WindRec definition */
  41. #include "rsmac.proto.h"        /* For RSgetwindow & RSnewwindow protos */
  42. #include "DlogUtils.proto.h"    /* For VersionNumber, UItemAssign, GetNewMyDialog protos */
  43. #include "Sets.proto.h"        // For readconfig proto
  44. #include "prefs.proto.h"
  45. #include "vr.h"            /* For VRinit proto */
  46. #include "vsdata.h"
  47. #include "vsinterf.proto.h"    /* For VSinit proto */
  48. #include "vgtek.proto.h"        /* For VGinit proto */
  49. #include "vrrgmac.proto.h"    /* For MacRGinit proto */
  50. #include "telneterrors.h"        /* for DoError proto */
  51. #include "tekrgmac.proto.h"    /* For RGMalloc proto */
  52. #include "menuseg.proto.h"
  53. #include "translate.proto.h"
  54. #include "dnr.proto.h"    // For OpenResolver proto
  55. #include "tnae.h"
  56. #include "authencrypt.h"
  57. #include "authencrypt.proto.h"
  58. #include "ae.proto.h"            // For AppleEvent handlers
  59. #include "MacBinary.h"
  60. #include "binsubs.proto.h"        // For init_mb_files proto
  61.  
  62.  
  63. /*--------------------------------------------------------------------------------*/
  64. #define CurrentVersion 1                /* Last known SysEnvirons version */
  65. #define HFSPtr ((long *)1014)
  66.  
  67. extern    char *tempspot;                /* for temp storage only */
  68.  
  69. extern    unsigned char
  70.     *macro[10];        /* the wonderful macro package .... */
  71.  
  72. extern    Cursor *theCursors[];
  73.  
  74. extern    WindRec
  75.     *screens,        /* Window Records (VS) for :    Current Sessions */
  76.     *ftplog;            /*                                FTP Log             */
  77. extern    MenuHandle    myMenus[];
  78.  
  79. OSErr        io;
  80. char        *mungbuf;
  81. SysEnvRec    theWorld;                        /* System Environment record */
  82. short        TempItemsVRefNum;
  83. long        TempItemsDirID;
  84. Boolean        gKeyboardHasControlKey, gAEavail;
  85.  
  86. Boolean encryptOK;                // true if des encryption routines available
  87. Boolean authOK;                    // true if authentication driver available
  88.  
  89. /*    checkCONF - check if the user clicked on a configuration file */
  90. void checkCONF( void)
  91. {
  92.     short    i,mess,count;
  93.     AppFile    theFile;
  94.     FSSpec    FileSpec;
  95.     long    junk = 0;
  96.  
  97.     if (gAEavail) return;    // If AppleEvents are available, we'll get ODOCs
  98.  
  99. #ifndef __powerpc__
  100.     CountAppFiles(&mess,&count);
  101.     if (mess==1) return;
  102.     if (count<1) return;
  103.     for(i=1;i<=count;i++) {
  104.         GetAppFiles(i, &theFile);
  105.         if (theFile.fType=='CONF') {
  106.             ClrAppFiles(i);
  107.             BlockMove(&theFile.fName, FileSpec.name, (*theFile.fName)+1); // pstring copy theFile.fName -> FileSpec.name
  108.             GetWDInfo(theFile.vRefNum, &FileSpec.vRefNum, &FileSpec.parID, &junk);
  109.             readconfig(FileSpec);    /* BYU LSC */
  110.             }
  111.         }
  112. #endif
  113.     return;
  114. }
  115.  
  116. void initmacros( void)
  117. {
  118.     short i;
  119.  
  120.     for (i=0; i<10 ; i++) {
  121.         macro[i] = (unsigned char *) NewPtrClear(256);        /* BYU LSC */
  122.         if (macro[i]== NULL) 
  123.             {            /* Cant allocate space for the Macros */
  124.             DoError(102 | MEMORY_ERRORCLASS, LEVEL3,NULL);    
  125.             forcequit();
  126.             }
  127.         setmacro( i, (unsigned char *) "");        /* BYU LSC */
  128.         }
  129. }
  130.  
  131. void initEvents( void)
  132. {
  133.     EventRecord myEvent;
  134.  
  135.     FlushEvents(everyEvent - diskEvt,0);    /* Don't Let 'em get off without seeing it. */
  136.  
  137.     WaitNextEvent(everyEvent, &myEvent, gApplicationPrefs->TimeSlice, 0L);    /* WNE always avail */
  138.     WaitNextEvent(everyEvent, &myEvent, gApplicationPrefs->TimeSlice, 0L);
  139. }
  140.  
  141. /* initftplog - initialize the FTP log window */
  142. void initftplog( void)
  143. {
  144.     Rect prect;
  145.  
  146.     SetRect(&prect, 300,256,512,384);
  147.  
  148.     ftplog = (WindRec *) NewPtrClear(sizeof(WindRec));
  149.     
  150.     ftplog->vs=RSnewwindow( &prect, 50, 80, 24,
  151.                     "\pFTP Log", 1, DefFONT, DefSIZE, gFTPServerPrefs->ShowFTPlog,
  152.                         1,0);    /* NCSA 2.5 */
  153.  
  154.     ftplog->wind = RSgetwindow( ftplog->vs);
  155.     ((WindowPeek)ftplog->wind)->windowKind = WIN_LOG;
  156.     
  157.     VSwrite(ftplog->vs,"\033[24;0H",7);        /* Move to bottom of screen */
  158.     ftplog->active=0;                         /* Meaningless */
  159.     ftplog->port=0;                            /* As is this */
  160.     ftplog->termstate=VTEKTYPE;
  161. }
  162.  
  163.   /* copies a specified cursor from a resource into a nonrelocatable block
  164.     and returns a pointer to it in *result. */
  165. void loadcursor( short CursorID, CursPtr *result)
  166. {
  167.     CursHandle tempcurs;
  168.     OSErr Err;
  169.  
  170.     *result = nil;
  171.     do /* once */
  172.       {
  173.         tempcurs = GetCursor(CursorID);
  174.         Err = ResError();
  175.         if (Err != noErr)
  176.             break;
  177.         *result = (CursPtr) NewPtr(sizeof(Cursor));        /* BYU LSC */
  178.         Err = MemError();
  179.         if (Err != noErr)
  180.             break;
  181.         LoadResource((Handle) tempcurs); /* in case it was purged */
  182.         Err = ResError();
  183.         if (Err != noErr)
  184.             break;
  185.         BlockMove((Ptr) *tempcurs, (Ptr) *result, sizeof(Cursor));
  186.       }
  187.     while (false);
  188.     if (Err != noErr)
  189.       {
  190.         if (*result != nil)
  191.             DisposPtr((Ptr) *result);
  192.         *result = &qd.arrow;
  193.       }
  194. } /* loadcursor */
  195.  
  196. /* initmcurs - Set up the mouse Cursors
  197.  *        NOTE: debug tells if the bug or the pointer is to be used as dflt.*/
  198. void initmcurs ( short debug) 
  199. {
  200.     loadcursor(rWatchCursor, &theCursors[watchcurs]);
  201.     loadcursor(rCrossCursor, &theCursors[graphcurs]);
  202.     loadcursor(rPOSCURS, &theCursors[poscurs]);
  203.     loadcursor(rBeamCursor, &theCursors[textcurs]);
  204.     loadcursor(rGINCURS, &theCursors[gincurs]);
  205.     loadcursor(rXferCURS, &theCursors[xfercurs]);
  206.     if (debug)
  207.         loadcursor(rDbugCURS, &theCursors[normcurs]);
  208.     else
  209.         theCursors[normcurs] = &qd.arrow;
  210.     SetCursor(theCursors[normcurs]);
  211. }
  212.  
  213. /*     If a file with the filetype "DBUG" was sent to us, set the debug mode. */
  214. short checkdebug( void)
  215. {
  216.     short i,mess,count;
  217.     AppFile theFile;
  218.     long junk = 0;
  219.  
  220.     if (gAEavail) return(FALSE);    //     Don't use CountAppFiles if AppleEvents
  221.                                     //    are available.
  222. #ifndef __powerpc__    
  223.     CountAppFiles(&mess,&count);
  224.     if (mess==1) return(FALSE);
  225.     if (count<1) return(FALSE);
  226.     for(i=1;i<=count;i++) {
  227.         GetAppFiles(i, &theFile);
  228.         if (theFile.fType=='DBUG') {
  229.             ClrAppFiles(i);
  230.             return(TRUE);
  231.             }
  232.         }
  233.     return(FALSE);
  234. #endif
  235. }
  236.  
  237. void InquireEnvironment( void)
  238. {
  239.     OSErr    err;
  240.     Boolean HFSflag;
  241.     long    response;
  242.     
  243.     long *HFSp=(long *)1014L;
  244.     
  245.     HFSflag= (*HFSp) > 0L;
  246.     if (!HFSflag) FatalAlert(NEED_HFS_ERR, 0, 0);
  247.  
  248.     err = SysEnvirons(CurrentVersion, &theWorld);
  249.     
  250.     if (err == envVersTooBig) FatalAlert(SYS_ENVIRON_ERR, 0, 0);
  251.     
  252.     if (theWorld.systemVersion < 0x0600) FatalAlert(SYSTEM_VERS_ERR, 0, 0);
  253.     
  254.     if (theWorld.machineType < 0) FatalAlert(ROM_VERS_ERR, 0, 0);
  255.     
  256.     // If there is a problem w/Gestalt, assume our keyboard has a Control key.
  257.     // Otherwise, we assume we have a control key unless a Mac or MacPlus keyboard is
  258.     // present.
  259.         
  260.     if ((err = Gestalt(gestaltKeyboardType, &response)) != noErr)
  261.         gKeyboardHasControlKey = TRUE;
  262.     else
  263.         gKeyboardHasControlKey = !(    (response == gestaltMacKbd) ||
  264.                                     (response == gestaltMacAndPad) ||
  265.                                     (response == gestaltMacPlusKbd) );
  266.                                     
  267.     /* System 6.0 and up ALWAYS has WNE trap! */
  268. }
  269.  
  270. void initmac( void)
  271. {
  272.     EventRecord myEvent;
  273.     short     i;
  274.     long    gestaltvalue;
  275.     OSErr    err;
  276.  
  277.     InitGraf(&qd.thePort);
  278.     InitFonts();
  279.     InitWindows();
  280.     InitMenus();
  281.     TEInit();
  282.     InitDialogs(0L);                /* No resume proc */
  283.     InitCursor();
  284.     
  285.     TelInfo = (TelInfoRec *)NewPtrClear(sizeof(TelInfoRec));
  286.     mungbuf = (char *)NewPtrClear(1024);
  287.     tempspot = (char *)NewPtrClear(256);
  288.     gApplicationPrefs = (ApplicationPrefs *)NewPtrClear(sizeof(ApplicationPrefs));
  289.     gFTPServerPrefs = (FTPServerPrefs *)NewPtrClear(sizeof(FTPServerPrefs));
  290.     screens = (WindRec *)NewPtrClear(MaxSess*sizeof(WindRec));
  291.  
  292.     InquireEnvironment();
  293.  
  294.     for (i=1;i<12;i++)
  295.         EventAvail( 0xffff, &myEvent);
  296.     
  297.     err = Gestalt(gestaltAppleEventsAttr, &gestaltvalue);        // See if AppleEvents are available
  298.     gAEavail = (!err && ((gestaltvalue >> gestaltAppleEventsPresent) & 0x0001));
  299.     
  300.     if (gAEavail) {
  301.         if ((err = AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,
  302.                                             MyHandleOAppUPP,0,FALSE)) != noErr)
  303.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  304.         if ((err = AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,
  305.                                             MyHandleODocUPP,0,FALSE)) != noErr)
  306.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  307.         if ((err = AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,
  308.                                             MyHandlePDocUPP,0,FALSE)) != noErr)
  309.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  310.         if ((err = AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
  311.                                             MyHandleQuitUPP,0,FALSE)) != noErr)
  312.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  313.         }
  314.  
  315.     TelInfo->screenRect = qd.screenBits.bounds;            /* well, they have to be set */
  316.                                                 /* somewhere, where else ? */
  317.     SetRect(&TelInfo->dragRect, 4, 24, TelInfo->screenRect.right-4,
  318.                         TelInfo->screenRect.bottom-4);
  319. }
  320.  
  321. void DoTheGlobalInits(void)
  322. {
  323.     long junk = 0;
  324.     ParamBlockRec pb;
  325.     
  326.     TelInfo->ScrlLock=0;
  327.     TelInfo->ftplogon=0;
  328.     TelInfo->done=0;
  329.     TelInfo->MacBinary=0;
  330.     TelInfo->numwindows=0;
  331.     TelInfo->CONFstate=0;
  332.     TelInfo->CONFactive=0;
  333.     TelInfo->SettingsFile=-1;
  334.     TelInfo->ApplicationFile = CurResFile();
  335.     TelInfo->ginon   = 0;
  336.     TelInfo->xferon  = 0;
  337.     TelInfo->graphs = 0;
  338.     TelInfo->debug = 1;
  339.     TelInfo->myfronttype = NO_WINDOW;
  340.     TelInfo->myfrontvs = 0;
  341.     TelInfo->suspended = FALSE;
  342.     TelInfo->myfrontwindow = 0L;    
  343.     TelInfo->myfrontRgn = 0L;    
  344.     TelInfo->lastCursor = 0L;
  345.     TelInfo->DefaultDirVRefNum = -1;
  346.     TelInfo->FTPClientVRefNum = -1;
  347.     TelInfo->DefaultDirDirID = 2;
  348.     TelInfo->FTPClientDirID = 2;
  349.     
  350.     pb.ioParam.ioNamePtr = 0L;
  351.     PBGetVol(&pb, FALSE);            /* see Tech Note 140 for implications of this */
  352.  
  353.     GetWDInfo(pb.ioParam.ioVRefNum, &(TelInfo->ApFolder.vRefNum), &(TelInfo->ApFolder.parID), &junk);
  354.     TelInfo->ApFolder.name[0] = 0;
  355.  
  356.     FindFolder( kOnSystemDisk, kSystemFolderType, kCreateFolder,
  357.                     &(TelInfo->SysFolder.vRefNum), &(TelInfo->SysFolder.parID));
  358.     TelInfo->SysFolder.name[0]=0;
  359.  
  360.     FindFolder( kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  361.                     &(TelInfo->PrefFolder.vRefNum), &(TelInfo->PrefFolder.parID));
  362.     TelInfo->PrefFolder.name[0]=0;
  363.     
  364.     FindFolder( kOnSystemDisk, kTemporaryFolderType, kCreateFolder, &TempItemsVRefNum, &TempItemsDirID);
  365. }
  366.  
  367. void init (void)
  368. {
  369.     DialogPtr     dtemp;        /* Used for dialog display */
  370.  
  371.     FlushEvents(everyEvent,0);
  372.     initmac();                /* initialize Macintosh stuff */
  373.     
  374.     DoTheGlobalInits();
  375.  
  376.     OpenPreferencesFile();
  377.     LoadPreferences();
  378.     UnloadSeg(&LoadPreferences);
  379.     
  380.     setupmenu(gApplicationPrefs->CommandKeys);
  381.  
  382.     dtemp = GetNewMyDialog(FirstDLOG, NULL, kInFront, (void *)ThirdCenterDialog);    /* opening dialog */
  383.     UItemAssign( dtemp, 2, VersionNumberUPP);
  384.     DrawDialog(dtemp);                                        /* while we init everything */
  385.  
  386.     trInit(myMenus[National]);    /* LU: Initialize translation routines; must be done */
  387.                                 /* LU: prior to checkdebug(), since even the debug      */
  388.                                 /* LU: output goes through the translation routines. */
  389.     UnloadSeg(&trInit);
  390.  
  391.     TelInfo->debug=checkdebug();        /* must return TRUE or FALSE */
  392.     
  393.     initmcurs(TelInfo->debug);        /* init the mouse cursor */
  394.     SetCursor(theCursors[watchcurs]);
  395.  
  396.     if (RGMalloc()) {        
  397.         DoError(103 | MEMORY_ERRORCLASS, LEVEL3, NULL);
  398.         forcequit();
  399.     }                        
  400.     UnloadSeg(&RGMalloc);
  401.     
  402.     VGalloc();                        /* BYU - save global space */
  403.     UnloadSeg(&VGalloc);
  404.     
  405.     if (VSinit(MaxSess+2)) {        /* initialize the Virtual Screen */
  406.         DoError(104 | MEMORY_ERRORCLASS, LEVEL3, NULL);
  407.         forcequit();
  408.     }
  409.     UnloadSeg(&VSinit);
  410.     
  411.     InitDebug();
  412.     initftplog();                    /* allocate/initialize ftp log screen */
  413.  
  414.     VGinit();                        /* initialize Virtual Graphics after VS so that */
  415.                                     /* init messages can come on console screen */
  416.     UnloadSeg(&VGinit);
  417.     
  418.     VRinit();
  419.     UnloadSeg(&VRinit);
  420.     
  421.     MacRGinit();
  422.     UnloadSeg(&MacRGinit);
  423.     
  424.     init_mb_files();                /* Initialize file id's to closed state */
  425.     UnloadSeg(init_mb_files);
  426.  
  427.     initnet();                        /* initialize network stuff */
  428.     switchMenus(gApplicationPrefs->CommandKeys);
  429.  
  430.     TelInfo->done = FALSE;            // We're not quitting yet.
  431.  
  432.     SetCursor(theCursors[normcurs]);
  433.  
  434.     initmacros();
  435. //    initEvents();
  436.  
  437.     io = OpenResolver(NULL);
  438.  
  439.     if (io) {                                
  440.         DialogPtr theErrDialog;
  441.         short itemhit = 0;
  442.  
  443.         theErrDialog = GetNewDialog(RESOLVERERROR, NULL, kInFront);
  444.         DrawDialog(theErrDialog);
  445.         while (!itemhit)
  446.             ModalDialog(DLOGwOKUPP,&itemhit);
  447.         DisposDialog(theErrDialog);
  448.         }
  449.     
  450.     updateMenuChecks();
  451.     DoTheMenuChecks();
  452.  
  453.     checkCONF();                     /* Did user click on a set? */
  454.  
  455.     DisposDialog(dtemp);            /* Remove the splashbox...  */
  456.  
  457.     encryptOK = authOK = true;
  458.     if (!auth_encrypt_init()) {
  459.         encryptOK = false;
  460.         authOK = false;
  461.     }
  462. }
  463.